#!/bin/bash
set -e

# start
# git clone https://gitee.com/catch6/server-pack.git && cd server-pack && chmod +x install.sh && ./install.sh

echo "----------------------------------- 配置选项 -----------------------------------"

# 是否替换软件源
echo "是否替换为阿里镜像源？(y/n)"
read -r mirrors

if [[ $mirrors == "y" ]]; then
  echo "----------------------------------- 替换为阿里镜像源 -----------------------------------"
  arch=$(dpkg --print-architecture)
  if [[ $arch == "arm64" ]]; then
    # arm64 架构
    cat <<EOF >/etc/apt/sources.list
deb https://mirrors.aliyun.com/ubuntu-ports jammy main restricted
deb https://mirrors.aliyun.com/ubuntu-ports jammy-updates main restricted
deb https://mirrors.aliyun.com/ubuntu-ports jammy universe
deb https://mirrors.aliyun.com/ubuntu-ports jammy-updates universe
deb https://mirrors.aliyun.com/ubuntu-ports jammy multiverse
deb https://mirrors.aliyun.com/ubuntu-ports jammy-updates multiverse
deb https://mirrors.aliyun.com/ubuntu-ports jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu-ports jammy-security main restricted
deb https://mirrors.aliyun.com/ubuntu-ports jammy-security universe
deb https://mirrors.aliyun.com/ubuntu-ports jammy-security multiverse
EOF
  else
    # amd64 架构
    cat <<EOF >/etc/apt/sources.list
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted
deb https://mirrors.aliyun.com/ubuntu/ jammy universe
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates universe
deb https://mirrors.aliyun.com/ubuntu/ jammy multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted
deb https://mirrors.aliyun.com/ubuntu/ jammy-security universe
deb https://mirrors.aliyun.com/ubuntu/ jammy-security multiverse
EOF
  fi
fi

echo "----------------------------------- 更新软件 -----------------------------------"
apt update && apt upgrade -y

echo "----------------------------------- 安装基础软件 -----------------------------------"
apt install -y zsh lrzsz zip unzip net-tools

echo "----------------------------------- 清理软件 -----------------------------------"
apt autoremove -y

echo "----------------------------------- 设置中国时区 -----------------------------------"
timedatectl set-timezone Asia/Shanghai

echo "----------------------------------- 切换为 zsh -----------------------------------"
chsh -s /usr/bin/zsh

echo "----------------------------------- 安装 oh-my-zsh -----------------------------------"
sh -c "$(curl -fsSL https://ghproxy.com/https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

echo "----------------------------------- 安装 zsh-autosuggestions -----------------------------------"
git clone https://ghproxy.com/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

echo "----------------------------------- 安装 zsh-syntax-highlighting -----------------------------------"
git clone https://ghproxy.com/https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

echo "----------------------------------- 配置 .zshrc -----------------------------------"
curl -fsSL https://gitee.com/catch6/server-pack/raw/master/oh-my-zsh/.zshrc -o ~/.zshrc

source ~/.zshrc

echo "----------------------------------- docker 安装 -----------------------------------"
sudo apt update && apt install -y ca-certificates curl gnupg

sudo install -m 0755 -d /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |
  sudo tee /etc/apt/sources.list.d/docker.list >/dev/null

sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
